﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Configuration;

public partial class WrAnno : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["group"] == null)
        {
            Response.Redirect("../Login.aspx");
        }
        else if ((int)Session["group"] != 3)
        {
            Response.Redirect("../Login.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int uid = 0;
        int aid = 0;
        int max = 0;
        bool i = false;
        string annoid = "";
        //read text
        string title = AnnoTitle.Text;
        string text = AnnoText.Text;
        //define db variables
        DateTime dt = DateTime.Now;
        SqlConnection sqlcon = null;
        SqlCommand sqlcmd = null;
        SqlCommand sqlcmd2 = null;
        SqlCommand sqlcmd3 = null;
        SqlCommand sqlcmd4 = null;
        SqlCommand sqlcmd5 = null;
        SqlDataReader read = null;
        string cmd = "";
        if(title=="")
        {
            titleerror.Text = "请输入标题！";
            texterror.Text = "";
        }
        else if (text == "")
        {
            titleerror.Text = "";
            texterror.Text = "请输入正文！";
        }
        else
        {
            try
            {
                sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testLottery"].ConnectionString);
                sqlcon.Open();
                cmd = "insert into Announcement(title,[content],time) values ('" + title + "','" + text + "','" + dt + "'   )";
                sqlcmd = new SqlCommand(cmd, sqlcon);
                //insert
                sqlcmd.ExecuteNonQuery();
                titleerror.Text = "";
                texterror.Text = "发布成功！";
                //get annoid
                cmd = "select max(id) from Announcement";
                sqlcmd2 = new SqlCommand(cmd, sqlcon);
                read = sqlcmd2.ExecuteReader();
                read.Read();
                aid = Int16.Parse(read.GetValue(0).ToString());
                read.Close();
                //get maxuid
                cmd = "select max(id) from Member";
                sqlcmd3 = new SqlCommand(cmd, sqlcon);
                read = sqlcmd3.ExecuteReader();
                read.Read();
                max = Int16.Parse(read.GetValue(0).ToString());
                read.Close();
                //insert into every member annoid
                for (uid = 0; uid <= max; uid++)
                {
                    cmd = "select Announcement from Member where id='" + uid + "'";
                    sqlcmd4 = new SqlCommand(cmd, sqlcon);
                    read = sqlcmd4.ExecuteReader();
                    i = read.Read();
                    read.Close();
                    if (i)
                    {
                        cmd = "select Announcement from Member where id='" + uid + "'";
                        sqlcmd4 = new SqlCommand(cmd, sqlcon);
                        read = sqlcmd4.ExecuteReader();
                        read.Read();
                        annoid = read.GetValue(0).ToString();
                        read.Close();
                        annoid = annoid + aid + ";";
                        cmd = "update member set Announcement='" + annoid + "' where id='" + uid + "'";
                        sqlcmd5 = new SqlCommand(cmd, sqlcon);
                        sqlcmd5.ExecuteNonQuery();
                    }
                    else
                    {
                        ;
                    }
                }
            }
            catch (SqlException sqle)
            {
                ;
            }
            finally
            {
                sqlcon.Close();
            }
        }
    }
}